package org.example.test;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import java.io.ByteArrayInputStream;
import java.util.UUID;

import static org.jacoco.agent.rt.internal_43f5073.core.runtime.AgentOptions.OutputMode.file;

public class AliYunOssDemo {

    private static  String accessKeyId = "LTAI5tHWGSZJsN3usJURFka2";
    private static String accessKeySecret = "oZvcBtVST2QaIW8MvX7dxDYImMAmu4";
    private static String endpoint = "https://oss-cn-guangzhou.aliyuncs.com";
    private static String bucketName = "java-luntan";

    public static void main(String[] args)  {

        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String objectName="11aaaa";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // UUID 避免文件覆盖
        objectName =objectName+UUID.randomUUID().toString()+".txt";

        //        //上传文件到 OSS
        //        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //        ossClient.putObject(bucketName, fileName, inputStream);
        //
        //        //文件访问路径
        //        String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
        //        // 关闭ossClient
        //        ossClient.shutdown();
        //        return url;// 把上传到oss的路径返回
            // 填写字符串。
            String content = "Hello OSS，你好世界";

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));

            // 上传字符串。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
            //文件访问路径
            String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + objectName;
            System.out.println(url);
            //关闭 链接
             ossClient.shutdown();

    }

}